#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
	int n, m;
	cin>>n>>m;
	int maxUnsortedNess = (n * n - n) / 2;
	string **result = new string*[maxUnsortedNess + 1];
	for (int i = 0; i < maxUnsortedNess + 1; i++) {
		result[i] = new string[m];
	}
	for (int i = 0; i < m; i++) {
		string s;
		cin>>s;

		int unsortedNess = 0;
		for (int p = 0; p < n - 1; p++) {
			for (int q = p + 1; q < n; q++) {
				if (s[p] > s[q])
					++unsortedNess;
			}
		}
		for (int j = 0; j < m; j++) {
			if (result[unsortedNess][j].size() == 0) {
				result[unsortedNess][j] = s;
				break;
			}
		}			
	}

	for (int i = 0; i < maxUnsortedNess + 1; i++) {
		for (int j = 0; j < m; j++) {
			if (result[i][j].size() != 0)
				cout<<result[i][j]<<endl;
		}
	}

	for (int i = 0; i < maxUnsortedNess + 1; i++) {
		delete [] result[i];
	}
	delete[] result;
	return 0;
}